// pages/invoiceTitle/invoiceTitle.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    // 抬头数据源
        titleList:[],
        // 对画框显示标记
        titleModalFlag:false,
        titleId:'',  // 删除那条数据的id
        companyName:'',  //企业名
        code:'',  //社会信用代码
        codeModalFlag:false,
        updataModalFlag:false,
        editId:'',
        upfptitle:'',
        uptaxno:'',
        // 抬头数据源
    textlist:[
    {
      upName: '单位',
    }],
    typeIndex:1,
    // 种类选择
    fptype: [{
      fptypeName: '增值税专用发票'
    },
    {
      fptypeName: '增值税电子发票'
    }
  ],
  fptypeindex:0,
  fptypeName:'',
    get1:null,
    get2:null,
    get3:null,
    get4:null,
    get5:null,
    get6:null,
    get7:null,
    get8:null,
    updatelist:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let _this = this
    let   token= wx.getStorageSync('token')
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx?cmd=saveorder',
      data: {
        cmd: 'FindFpTitle',
        token: token,
       
      },
      method: "get",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        console.log(res);
        _this.setData({
          titleList:res.data.DATA
        })
      }
    })

  },

 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  navigateBack(){
    wx.switchTab({
       url: '/pages/me/me',
     })
  },
  // 删除title
  deleteTitle(e){
    // 获取当前要删除那条的数据的id
    this.setData({
       titleId:e.currentTarget.dataset.id
    })
    // 对话框打开
    this.setData({
      titleModalFlag:true
    })
  },
  // 对话框背景/或者取消点击关闭对话框
  colseLastModal(){
    this.setData({
      titleModalFlag:false
    })
  },
  //确认删除
  submitDelete(){
    let that = this
    let token= wx.getStorageSync('token')
    let titleId = that.data.titleId;
    let titleModalFlag = that.data.titleModalFlag;
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx?cmd=DelFpTitle',
      data: {
        token: token,
        TitleID:titleId
      },
      method: "get",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
         if(res.data.STATUS=='Success'){
           wx.showToast({
            title: res.data.DESC,
            icon: 'success',
            duration: 2000
          }) 
          
          setTimeout(function(){
            wx.request({
              url: 'https://www.yulonging.com/dwweb/wxapi.ashx?cmd=saveorder',
              data: {
                cmd: 'FindFpTitle',
                token: token,
               
              },
              method: "get",
              header: {
                'content-type': 'application/json' // 默认值
              },
              success (res) {
                console.log(res);
                that.setData({
                  titleList:res.data.DATA,
                  titleModalFlag:false
                })
              }
            })
          },1000)
                 
        }else{
          wx.showToast({
            title: res.data.DESC,
            icon: 'error',
            duration: 2000
          })
        }
      }
    })


      //删除完毕关闭对话框
      this.setData({
        titleModalFlag:false
      })
  },
  // 获取企业名
  getCompanyName(e){
        this.setData({
          companyName: e.detail.value
        })
  },
  //获取社会信用代码
  getCode(e){
      this.setData({
        code:e.detail.value
      })
  },
  // 发票抬头编写按钮确定
  subMitClick(){
        // console.log('确定点击了');
        // this.setData({
        //   codeModalFlag:false
        // })

    let that = this
    let   token= wx.getStorageSync('token')
    let code = that.data.code
    let companyName = that.data.companyName;
    let codeModalFlag = that.data.codeModalFlag;
    console.log(code);
    console.log(companyName)
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx?cmd=saveorder',
      data: {
        cmd: 'SaveFpTitle',
        token: token,
        TitleID:0,
        IsCorp:1,
        FpTitle:companyName,
        TaxNo:code,
        FpType:0,
        Email:''
      },
      method: "get",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        if(res.data.STATUS=='Success'){
          wx.showToast({
            title: res.data.DESC,
            icon: 'success',
            duration: 2000
          })
          setTimeout(function(){
            wx.request({
              url: 'https://www.yulonging.com/dwweb/wxapi.ashx?cmd=saveorder',
              data: {
                cmd: 'FindFpTitle',
                token: token,
               
              },
              method: "get",
              header: {
                'content-type': 'application/json' // 默认值
              },
              success (res) {
                console.log(res);
                that.setData({
                  titleList:res.data.DATA,
                  codeModalFlag:false
                })
              }
            })
          },1000)
        }else{
          wx.showToast({
            title: res.data.DESC,
            icon: 'error',
            duration: 2000
          })
        }

      }
    })





  },
  // 编辑按钮点击
  editTitle(e){
  
    this.setData({
      codeModalFlag:true,
      editId:e.currentTarget.dataset.id
    })
  },
  
  // 编辑对话框背景点击
  colseEditModal(){
    this.setData({
      codeModalFlag:false
    })
  },

  // 修改按钮点击
  updataTitle(e){
    // 打开编辑对话框,编辑id赋值
    let that = this;
    let token= wx.getStorageSync('token')
    this.setData({
      updataModalFlag:true,
      editId:e.currentTarget.dataset.id,
    })
    let editId = e.currentTarget.dataset.id
    console.log(editId)
    // 打开编辑对话框,编辑id赋值
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
      data: {
        cmd: 'FindFpTitle',
        token: token,
      },
      method: "get",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        res.data.DATA.forEach((item, index) => {
          if(item.TitleID==editId){
          let   updatelist = item
          that.setData({
            updatelist:updatelist,
            fptypeindex:updatelist.FpType,
            typeIndex:updatelist.IsCorp,

            get1:updatelist.FpTitle,
            get2:updatelist.TaxNo,
            get4:updatelist.EMail,
            get5:updatelist.Bank,
            get6:updatelist.BankCard,
            get7:updatelist.Address,
            get8:updatelist.Tel,
          })
          }
        })
          
         console.log(that.data.updatelist);
      }
    })
    
  },
  // onswitchup(e){
  //   console.log(e.currentTarget.dataset.index);
  //   let that = this;
  //   that.setData({
  //     typeIndex:e.currentTarget.dataset.index,
  //   })
  // },
  // 修改对话框背景点击
  updataEditModal(){
    this.setData({
      updataModalFlag:false
    })
  },
  adddataEditModal(){
    this.setData({
      codeModalFlag:false
    })
  },

    get1(e){
      console.log(e)
      this.setData({
        get1:e.detail.value,
      })
    },
    get2(e){
      console.log(e)
      this.setData({
        get2:e.detail.value,
      })
    },
    get3(e){
      console.log(e)
      this.setData({
        get3:e.detail.value,
      })
    },
    get4(e){
      console.log(e)
      this.setData({
        get4:e.detail.value,
      })
    },
    get5(e){
      console.log(e)
      this.setData({
        get5:e.detail.value,
      })
    },
    get6(e){
      console.log(e)
      this.setData({
        get6:e.detail.value,
      })
    },
    get7(e){
      console.log(e)
      this.setData({
        get7:e.detail.value,
      })
    },
    get8(e){
      console.log(e)
      this.setData({
        get8:e.detail.value,
      })
    },




  updataMitClick(e){
    let that = this
    let token= wx.getStorageSync('token')
    let code = that.data.code
    let companyName = that.data.companyName
    let updataModalFlag=that.data.updataModalFlag;
    let editId = that.data.editId;
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx?',
      data: {
        cmd: 'SaveFpTitle',
        token: token,
        TitleID:editId,
        IsCorp:1,
        FpTitle:companyName,
        TaxNo:code,
        FpType:0,
        Email:''
      },
      method: "get",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
        if(res.data.STATUS=='Success'){
            wx.showToast({
              title: res.data.DESC,
              icon: 'success',
              duration: 2000
            })
            setTimeout(function(){
              wx.request({
                url: 'https://www.yulonging.com/dwweb/wxapi.ashx?cmd=saveorder',
                data: {
                  cmd: 'FindFpTitle',
                  token: token,
                
                },
                method: "get",
                header: {
                  'content-type': 'application/json' // 默认值
                },
                success (res) {
                  console.log(res);
                  that.setData({
                    titleList:res.data.DATA,
                    updataModalFlag:false
                  })
                }
              })
            },1000)
     
        }else{
          wx.showToast({
            title: res.data.DESC,
            icon: 'error',
            duration: 2000
          })
        }

      }
    })

  },
  selectfptype(e){
    // 切换样式,选择了普通还是企业
    console.log(e);
    this.setData({
      fptypeindex: e.currentTarget.dataset.index,
      fptypeName: e.currentTarget.dataset.name
    })
  },
  oklinfo(e){
    let that  = this  
    let  typeIndex = that.data.typeIndex;
    let fptypeindex = that.data.fptypeindex;
    let token= wx.getStorageSync('token')
    let  get1 = that.data.get1
    let  get2 = that.data.get2
    let  get3 = that.data.get3
    let  get4 = that.data.get4
    let  get5 = that.data.get5
    let  get6 = that.data.get6
    let  get7 = that.data.get7
    let  get8 = that.data.get8
    console.log(get8);
    // 打开编辑对话框,编辑id赋值
    if(get1==null ){
      wx.showToast({icon: 'none', title: '请填写单位名称',duration: 1500 })
    }else if(get2==null){
      wx.showToast({icon: 'none', title: '请输入税号',duration: 1500 })
    }else if(get4==null || !(/^[a-zA-Z0-9_.-]+@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.[a-zA-Z0-9]{2,6}$/.test(get4))){ 
      wx.showToast({ title: '请输入正确的邮箱', icon: 'none', duration: 1500 }) 
    } else if (get5==null) {
      wx.showToast({ title: '请输入银行名称', icon: 'none', duration: 1500 }) 
    }else if (get6==null ) {
      wx.showToast({ title: '请输入银行账户', icon: 'none', duration: 1500 }) 
    }else if (get7==null ) {
      wx.showToast({ title: '请输入单位地址', icon: 'none', duration: 1500 }) 
    }else if (get8==null || !(/^1[34578]\d{9}$/.test(get8))) {
      wx.showToast({ title: '请输入正确的手机号', icon: 'none', duration: 1500 }) 
    }
    else{
      wx.request({
        url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
        data: {
          cmd: 'SaveFpTitle',
          token: token,
          TitleID:0,
          IsCorp:typeIndex,
          FpTitle:get1,
          TaxNo:get2,
          FpType:fptypeindex,
          Email:get4,
          Bank:get5,
          BankCard:get6,
          Address:get7,
          Tel:get8,
        },
        method: "get",
        header: {
          'content-type': 'application/json' // 默认值
        },
        success (res) {
            console.log(res);
            if(res.data.STATUS=='Success'){
              wx.showToast({
                title: res.data.DESC,
                icon: 'success',
                duration: 2000
              })
              setTimeout(function () {
                //要延时执行的代码
                wx.navigateTo({
                  url: '../invoiceTitle/invoiceTitle',
                })

              }, 1000) //延迟时间 这里是1秒   
            }else{
              wx.showToast({
                title: res.data.DESC,
                icon: 'error',
                duration: 2000
              })
            }
          
        }
      })
    }
  },
  /*修改*/
  uplinfo(e){
    let that  = this  
    let  typeIndex = that.data.typeIndex;
    let fptypeindex = that.data.fptypeindex;
    let token= wx.getStorageSync('token')
    let FpTitle = that.data.get1;
    let TaxNo = that.data.get2;
    let Email = that.data.get4;
    let Bank = that.data.get5;
    let BankCard = that.data.get6;
    let editId = that.data.editId;
    let Address = that.data.get7;
    let Tel = that.data.get8;
    // 打开编辑对话框,编辑id赋值
    wx.request({
      url: 'https://www.yulonging.com/dwweb/wxapi.ashx',
      data: {
        cmd: 'SaveFpTitle',
        token: token,
        TitleID:editId,
        IsCorp:typeIndex,
        FpTitle:FpTitle,
        TaxNo:TaxNo,
        FpType:fptypeindex,
        Email:Email,
        Bank:Bank,
        BankCard:BankCard,
        Address:Address,
        Tel:Tel,
      },
      method: "get",
      header: {
        'content-type': 'application/json' // 默认值
      },
      success (res) {
          console.log(res);
          if(res.data.STATUS=='Success'){
            wx.showToast({
              title: res.data.DESC,
              icon: 'success',
              duration: 2000
            })
            setTimeout(function () {
              //要延时执行的代码
              wx.navigateTo({
                url: '../invoiceTitle/invoiceTitle',
              })

             }, 1000) //延迟时间 这里是1秒   
          }else{
            wx.showToast({
              title: res.data.DESC,
              icon: 'error',
              duration: 2000
            })
          }
        
      }
    })
  }


})